//
//  TGMainViewController.m
//  GroupBuy
//
//  Created by wwj on 15/10/30.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGMainViewController.h"
#import "TGGroupListController.h"
#import "TGNavigationController.h"
#import "TGCollectController.h"
#import "TGMapController.h"
#import "TGMineController.h"
#import "TGDock.h"

@interface TGMainViewController () <DockDelegate>
@property (strong, nonatomic) TGNavigationController *naVC;
@property (weak ,nonatomic) UIView *contentView;
@end

@implementation TGMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TGDock *doct = [TGDock dock];
    doct.delegate = self;
    doct.frame = CGRectMake(0, 0, 0, self.view.frame.size.height);
    [self.view addSubview:doct];
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.frame = CGRectMake(kDockW, 0, self.view.frame.size.width - kDockW, self.view.frame.size.height);
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:contentView];
    _contentView = contentView;
    
    //初始化控制器
    [self initVC];
}

#pragma -mark 初始化控制器
- (void)initVC{
    //团购
    [self addChildVC:[[TGGroupListController alloc] init]];
    
    //地图
    [self addChildVC:[[TGMapController alloc] init]];
    
    //收藏
    [self addChildVC:[[TGCollectController alloc] init]];
    
    //我的
    [self addChildVC:[[TGMineController alloc] init]];
    
    //默认选中团购
    [self onClickDockTab:nil from:0 to:0];
}


#pragma -mark 添加子控制器
- (void)addChildVC:(UIViewController *)childVC{
    _naVC = [[TGNavigationController alloc] initWithRootViewController:childVC];
    [self addChildViewController:_naVC];
}

#pragma -mark TGDock 代理
-(void)onClickDockTab:(TGDock *)dock from:(NSInteger)from to:(NSInteger)to{
    UIViewController *oldVC = self.childViewControllers[from];
    [oldVC.view removeFromSuperview];
    
    UIViewController *newVC = self.childViewControllers[to];
    newVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    newVC.view.frame = _contentView.bounds;
    [_contentView addSubview:newVC.view];
}
@end
